#include "tlv_writer.h"
#include "tlv_stream_reader.h"
#include "tlv_parser.h"
#include <iostream>

void read(TLVStreamReader stream){
     while (stream.hasNext()) {
        auto item = stream.next();
        if (!item) break;

        std::cout << "Type: " << static_cast<int>(item->type)
                  << ", Length: " << item->length
                  << ", Value: ";

        if (item->type == TLVType::STRING) {
            std::string s(item->value.begin(), item->value.end());
            std::cout << s;
        } else {
            for (auto byte : item->value)
                std::cout << std::hex << static_cast<int>(byte) << " ";
        }
        std::cout << std::endl;
    }
}

void read(TLVParser parser){
   parser.parse();

    auto strings = parser.getByType(TLVType::STRING);
    for (const auto& item : strings) {
        std::string s(item.value.begin(), item.value.end());
        std::cout << "STRING: " << s << std::endl;
    }

    auto firstUint8 = parser.getFirst(TLVType::UINT8);
    if (firstUint8) {
        std::cout << "First UINT8: " << static_cast<int>(firstUint8->value[0]) << std::endl;
    }
}

int main() {
    TLVBuffer buffer(1024);
    TLVWriter writer(buffer);

    writer.writeUint8(1);
    writer.writeUint8(2);
    writer.writeUint32(20250903);
    writer.writeString("Paul");

    TLVStreamReader stream(buffer);
    read(stream);
    TLVParser parser(buffer);
    read(parser);
    return 0;
}
